package com.jcark.ui.font;

import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.widget.TextView;

import java.util.HashMap;

/**
 * Description: 外部字体管理类<br>
 * 要确保每一种字体只加载一次，因为多次加载会造成内存不断增加，易引起内在泄漏
 *
 * @author King.Zi<br>
 * @date 2022/01/04<br>
 * Email: 78688689@qq.com
 */
public class FontManager {
    private static final int MIN_CUSTOM_TYPE_FACE = 100;
    private static final HashMap<Integer, String> sFontMap = new HashMap();
    public static final String DIN_BLACK = "DIN-Black.otf";
    public static final String DIN_BLACK_ALTERNATE = "DIN-BlackAlternate.otf";
    public static final String DIN_BOLD = "DIN-Bold.otf";
    public static final String DIN_BOLD_ALTERNATE = "DIN-BoldAlternate.otf";
    public static final String DIN_LIGHT = "DIN-Light.otf";
    public static final String DIN_LIGHT_ALTERNATE = "DIN-LightAlternate.otf";
    public static final String DIN_MEDIUM = "DIN-Medium.otf";
    public static final String DIN_MEDIUM_ALTERNATE = "DIN-MediumAlternate.otf";
    public static final String DIN_REGULAR = "DIN-Regular.otf";
    public static final String DIN_REGULAR_ALTERNATE = "DIN-RegularAlternate.otf";
    public static final String ICON_FONT = "iconfont.otf";
    private HashMap<String, Typeface> mFontCache = new HashMap();

    public FontManager() {
    }

    public static FontManager getInstance() {
        return FontManager.SingletonHolder.instance;
    }

    public Typeface getTypeface(Context context, String fontName) {
        Typeface typeface = (Typeface)this.mFontCache.get(fontName);
        if (null == typeface) {
            try {
                typeface = Typeface.createFromAsset(context.getAssets(), "font/" + fontName);
            } catch (Exception var5) {
                Log.e("FontManager", "load font has exception the message is " + var5.getMessage());
            }

            this.mFontCache.put(fontName, typeface);
        }

        return typeface;
    }

    public void setTypeface(Context context, TextView textView, int typeFaceValue) {
        if (typeFaceValue < 100) {
            switch (typeFaceValue) {
                case 0:
                    textView.setTypeface((Typeface)null, Typeface.NORMAL);
                    break;
                case 1:
                    textView.setTypeface((Typeface)null, Typeface.BOLD);
                    break;
                case 2:
                    textView.setTypeface((Typeface)null, Typeface.ITALIC);
                    break;
                case 3:
                    textView.setTypeface((Typeface)null, Typeface.BOLD_ITALIC);
            }
        } else {
            textView.setTypeface(this.getTypeface(context, (String)sFontMap.get(typeFaceValue)));
        }
    }

    static {
        sFontMap.put(100, "DIN-Black.otf");
        sFontMap.put(101, "DIN-BlackAlternate.otf");
        sFontMap.put(102, "DIN-Bold.otf");
        sFontMap.put(103, "DIN-BoldAlternate.otf");
        sFontMap.put(104, "DIN-Light.otf");
        sFontMap.put(105, "DIN-LightAlternate.otf");
        sFontMap.put(106, "DIN-Medium.otf");
        sFontMap.put(107, "DIN-MediumAlternate.otf");
        sFontMap.put(108, "DIN-Regular.otf");
        sFontMap.put(109, "DIN-RegularAlternate.otf");
    }

    private static class SingletonHolder {
        private static FontManager instance = new FontManager();

        private SingletonHolder() {
        }
    }
}
